package com.cv.platform.dal.mapper.subscribe;

import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.mybatis.core.mapper.BaseMapperX;
import com.cv.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.platform.controller.platform.subscribe.vo.CvSubscribePageReqVO;
import com.cv.platform.controller.platform.subscribe.vo.CvSubscribePageRespVO;
import com.cv.platform.controller.platform.subscribe.vo.CvSubscribeReqVO;
import com.cv.platform.controller.platform.subscribe.vo.CvSubscribeRespVO;
import com.cv.platform.dal.dataobject.subscribe.CvSubscribeDO;
import com.cv.platform.dal.dataobject.video.CvVideoDO;
import com.cv.platform.enums.VideoStatusEnum;
import com.cv.platform.enums.YesOrNoEnum;
import org.apache.ibatis.annotations.Mapper;

/**
 * 用户订阅数据层
 *
 * @author Charles_XDXD
 */
@Mapper
public interface CvSubscribeMapper extends BaseMapperX<CvSubscribeDO> {

    default PageResult<CvSubscribeRespVO> selectPage(CvSubscribeReqVO reqVO) {
        return selectJoinPage(reqVO, CvSubscribeRespVO.class, new MPJLambdaWrapperX<CvSubscribeDO>()
                .selectAll(CvSubscribeDO.class)
                .eq(CvSubscribeDO::getSubscribeUserId, SecurityFrameworkUtil.getLoginUserId())
                .selectAs(CvSubscribeDO::getTargetUserId, CvSubscribeRespVO::getUserId)
                .leftJoin(CvVideoDO.class, CvVideoDO::getUserId, CvSubscribeDO::getTargetUserId)
                .selectAs(CvVideoDO::getId, CvSubscribeRespVO::getVideoId)
                .selectAs(CvVideoDO::getTitle, CvSubscribeRespVO::getTitle)
                .selectAs(CvVideoDO::getThumbnail, CvSubscribeRespVO::getThumbnail)
                .selectAs(CvVideoDO::getViewCount, CvSubscribeRespVO::getViewCount)
                .selectAs(CvVideoDO::getCommentCount, CvSubscribeRespVO::getCommentCount)
                .selectAs(CvVideoDO::getCreateTime, CvSubscribeRespVO::getPublishTime)
                .eq(CvVideoDO::getIsVisible, YesOrNoEnum.YES.getCode())
                .eq(CvVideoDO::getStatus, VideoStatusEnum.PASS.getStatus())
                .groupBy(CvSubscribeDO::getId)
                .groupBy(CvVideoDO::getId)
                .orderByDesc(CvVideoDO::getCreateTime)
        );
    }
    
    default PageResult<CvSubscribePageRespVO> selectFansPage(CvSubscribePageReqVO reqVO) {
        return selectJoinPage(reqVO, CvSubscribePageRespVO.class, new MPJLambdaWrapperX<CvSubscribeDO>()
                .selectAll(CvSubscribeDO.class)
                .eq(CvSubscribeDO::getTargetUserId, reqVO.getUserId())
                .selectAs(CvSubscribeDO::getSubscribeUserId, CvSubscribePageRespVO::getUserId)
        );
    }

    default PageResult<CvSubscribePageRespVO> selectFollowersPage(CvSubscribePageReqVO reqVO) {
        return selectJoinPage(reqVO, CvSubscribePageRespVO.class, new MPJLambdaWrapperX<CvSubscribeDO>()
                .selectAll(CvSubscribeDO.class)
                .eq(CvSubscribeDO::getSubscribeUserId, reqVO.getUserId())
                .selectAs(CvSubscribeDO::getTargetUserId, CvSubscribePageRespVO::getUserId)
        );
    }

}
